The first instance of obj whose box overlaps the circle centered on (x,y) with the given radius, or nothing.
Works differently on Game Boy - On the Game Boy this call looks at every object of that kind one by one and is slow when there are many (asked of a parent object, every child kind too): use it a few times a frame at most. Distances count to 1/16 of a pixel.
Works differently on Game Boy Color - On the Game Boy Color this call looks at every object of that kind one by one and is slow when there are many (asked of a parent object, every child kind too): use it a few times a frame at most. Distances count to 1/16 of a pixel.
On other consoles this differs: see Side by side and How the consoles differ.
collision_circle(x, y, radius, obj) -> ref<object>| Name | Type |
|---|---|
x | real |
y | real |
radius | real |
obj | object |
ref<object>
The first instance of obj whose box overlaps a circle, or nothing. The circle is centered on (x, y) and radius is how far it reaches in every direction, in pixels. Use it for a round area: an explosion's blast, a bat's hearing range, a pickup magnet.
x and y are a place in the room, counted from the room's top-left corner.
The circle is tested against each instance's box (the same box collision_point and collision_rect use). It hits when the nearest point of the box is closer to (x, y) than radius. That has some consequences worth knowing:
If obj is a parent, the answer can be one of its children. With obj_enemy as the parent of obj_slime, collision_circle(x, y, radius, obj_enemy) can hand back a slime. The reference is typed as obj_enemy, so you can use the variables and events obj_enemy has through it. The parent's own instances are searched first, then each child's.
Two things to know, the same as for collision_rect:
nothing when there is none. Which one is first depends on where the game keeps the instances, not on the order you made them in, so do not build on it. If you need the closest one, use instance_nearest.instance_destroy), so "find one, destroy it, look again" never ends.To reach everything in a circle, go through the instances with with and compare each one's own x and y with the center, using dist_to. That reaches each one exactly once, though it compares the instance's middle point, not its whole box. The second example below does this.
It looks at every live instance of obj, like every other collision question. Measured by running the real engine code, with 32 instances of obj alive one call costs about 9,000 cycles. The console does about 281,000 cycles a frame, so that is about 3% of one. It is roughly half as much again as collision_rect and a little cheaper than collision_line. A few calls a frame against a few dozen instances is fine; one call from every instance every frame is not.
var hit = collision_circle(x, y, 24, obj_doc_enemy);
if (hit != nothing) {
instance_destroy(hit);
}The instant this spark appears, destroys the first obj_doc_enemy whose box reaches within 24 pixels of it - a round blast, not a square one.
with (obj_doc_enemy) {
if (dist_to(x, y, other.x, other.y) < 24) {
instance_destroy(self);
}
}Every obj_doc_enemy whose middle is within 24 pixels of the spark is destroyed, however many there are. This is the way to hit everything in the circle.
The circle is tested against each instance’s BOX. It is not tested against a circle drawn round the box, and not against the square that just fits round your circle: it hits when the nearest point of the box is closer to (x,y) than the radius. So a circle that only clips one corner of a box hits only if it really reaches that corner, and a circle centered inside a box always hits that box. Only touching does not count, the same as two boxes that just share an edge. A radius of 0 or less covers nothing, so it never hits, and a box with no size is never hit either. If obj is a parent, a child instance can be the answer: the parent’s own pool is searched first, then each child’s, and the result is typed as obj. Two caveats, the same as collision_rect: you get the FIRST one only (and which one is first is not the order you made them in), and an instance destroyed earlier in this same frame is still found (see instance_destroy). So "hit everything in this circle" cannot be written from the circle’s side. Sweep from the instance side instead - `with (obj_enemy) { if (dist_to(x, y, other.x, other.y) < 40) { ... } }` - which reaches every match exactly once, but compares each instance’s own x and y with the center, not its whole box. Cost: one quick test for each live instance of obj. Measured with 32 instances of obj alive it is about 9,000 cycles, 3 percent of a frame (about 281,000 cycles) - roughly half as much again as collision_rect, and a little cheaper than collision_line.